package edu.zqu.collegeField.project.controller;


import cn.hutool.core.util.StrUtil;
import edu.zqu.collegeField.project.po.Area;
import edu.zqu.collegeField.project.service.AreaService;
import edu.zqu.collegeField.project.vo.AreaVo;
import edu.zqu.common.result.ZQUResult;
import edu.zqu.common.status.HttpStatus;
import edu.zqu.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.sql.SQLException;
import java.util.List;

/**
 * <p>
 * 功能区 前端控制器
 * </p>
 *
 * @author 廖森
 * @since 2020-06-28
 */
@RestController
@RequestMapping("/api")
@Api(tags = "功能区")
public class AreaController {
    @Autowired
    private AreaService areaService;

    @ApiOperation(value = "获取功能区列表")
    @GetMapping(value = "/areas")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "schoolId", value = "校区id", paramType = "query", required = false),
            @ApiImplicitParam(name = "name", value = "功能区名称", paramType = "query", required = false)
    })
    public ZQUResult get(BigInteger collegeId,
                         BigInteger schoolId,
                         String name
    ) {
        List<AreaVo> areaVoList = areaService.get(collegeId, schoolId, StrUtil.trimToNull(name));
        if (StringUtils.isNotEmpty(areaVoList)) {
            return new ZQUResult(HttpStatus.SUCCESS, "请求成功", areaVoList);
        } else return new ZQUResult(HttpStatus.NO_CONTENT, "请求失败",null);
    }

    @ApiOperation(value = "获取功能区信息", response = AreaVo.class)
    @GetMapping(value = "/area/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "功能区id", paramType = "path", required = true)
    })
    public ZQUResult getById(@PathVariable BigInteger id) {
        AreaVo areaVo = areaService.getById(id);
        if (StringUtils.isNotNull(areaVo)) {
            return new ZQUResult(HttpStatus.SUCCESS, "请求成功", areaVo);
        }
        return new ZQUResult(HttpStatus.NO_CONTENT, "请求失败",null);
    }

    @ApiOperation(value = "添加")
    @PostMapping(value = "/area")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Area", name = "Area", value = "功能区对象", paramType = "query", required = true)
    })
    public ZQUResult add(@RequestBody Area area) {
        int rs = areaService.add(area);
        if (rs >= 1) {
            return new ZQUResult(HttpStatus.SUCCESS, "添加成功",null);
        }
        return new ZQUResult(HttpStatus.ERROR, "添加失败",null);
    }

    @ApiOperation(value = "更新")
    @PutMapping(value = "/area")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Area", name = "Area", value = "功能区对象", paramType = "query", required = true)
    })
    public ZQUResult update(@RequestBody Area area) {
        int rs = areaService.update(area);
        if (rs >= 1) {
            return new ZQUResult(HttpStatus.SUCCESS, "修改成功",null);
        }
        return new ZQUResult(HttpStatus.ERROR, "修改失败",null);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping(value = "/area/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "功能区id", paramType = "path", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult delete(@PathVariable BigInteger id, BigInteger byWho) throws SQLException {
        int rs = areaService.delete(id, byWho);
        if (rs >= 1) {
            return new ZQUResult(HttpStatus.SUCCESS, "删除成功",null);
        }
        return new ZQUResult(HttpStatus.ERROR, "删除失败",null);
    }
}

